package random.fox.p2p;

import android.app.Application;
import android.test.ApplicationTestCase;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import random.fox.p2p.api.ApiClient;

/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
public class ApplicationTest extends ApplicationTestCase<Application> {
    private String TAG = getClass().getSimpleName();

    public ApplicationTest() {
        super(Application.class);
    }

    public void test() {
        RequestQueue mQueue = Volley.newRequestQueue(getContext());
        String host = "http://192.168.199.109:3000";
        String url = host + "/register";
//        mQueue.add(new JsonObjectRequest(Request.Method.GET, url, null,
//                new Response.Listener<JSONObject>() {
//                    @Override
//                    public void onResponse(JSONObject jsonObject) {
//                        Log.d(TAG, jsonObject + "");
//                    }
//
//                }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//                Log.d(TAG, volleyError.toString());
//            }
//        }));

        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.d(TAG, s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = super.getParams();
                if (params == null)
                    params = new HashMap();
                if (!params.containsKey("name")) {
                    params.put("name", "test");
                }
                if (!params.containsKey("password")) {
                    params.put("password", "test");
                }
                return params;
            }
        };
        mQueue.add(request);

        mQueue.start();

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    String info;

    public void test4() {
        ApiClient apiClient = new ApiClient(Volley.newRequestQueue(getContext()));
        apiClient.test(new ApiClient.CallBack() {
            @Override
            public void onResponse(Object o) {
                info = o + "";
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                info = error.toString();
            }
        });
        while (TextUtils.isEmpty(info)) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Log.d(TAG,info);
    }

    public void test5(){
        String str = "http://211.151.13.46:80/login";
        try {
            URL url = new URL(str);
            URLConnection connection = url.openConnection();
            connection.connect();
            OutputStream outputStream = connection.getOutputStream();
            int length = connection.getContentLength();
            byte[] buffer = new byte[length];
            outputStream.write(buffer);
            Log.d(TAG,new String(buffer));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}